package com.demo.cloud.shiro.serivce;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.cloud.shiro.bean.Permissions;
import com.demo.cloud.shiro.bean.Role;
import com.demo.cloud.shiro.bean.User;
import com.demo.cloud.shiro.dao.UserDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Override
    public int createUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public IPage<User> findList() {
        IPage<User> page= new Page(1,3);
        IPage<User>  users = userDao.findList(page);
        return users;
    }


    @Override
    public User getUserByUsername(String getMapByName) {
        return getMapByName(getMapByName);
    }

    /**
     * 模拟数据库查询
     *
     * @param userName 用户名
     * @return User
     */
    private User getMapByName(String userName) {
        Permissions permissions1 = new Permissions("1", "query");
        Permissions permissions2 = new Permissions("2", "add");
        Permissions permissions3 = new Permissions("3", "mv");
        Set<Permissions> permissionsSet = new HashSet<>();
        permissionsSet.add(permissions1);
        permissionsSet.add(permissions2);
        permissionsSet.add(permissions3);
        Role role = new Role("1", "admin", permissionsSet);
        Set<Role> roleSet = new HashSet<>();
        roleSet.add(role);
        User user = new User("1", "wsl", "123456");
        Map<String, User> map = new HashMap<>();
        map.put(user.getUserName(), user);
//
//        Set<Permissions> permissionsSet1 = new HashSet<>();
//        permissionsSet1.add(permissions1);
//        Role role1 = new Role("2", "user", permissionsSet1);
//        Set<Role> roleSet1 = new HashSet<>();
//        roleSet1.add(role1);
//        User user1 = new User("2", "zhangsan", "123456", roleSet1);
//        map.put(user1.getUserName(), user1);
        return map.get(userName);
    }
}